package LeetCode.month03.day71;


/**
 * @author 16778
 * @date 2025/10/10 8:10
 * @description
 */
public class No3147 {
    static void main() {
        maximumEnergy(new int[]{7,-9,4,1,10,7,9,-3},6);
    }
    public static int maximumEnergy(int[] energy, int k) {
        int left = 0,right = energy.length-1;
        while(left<right){
            int temp  =energy[left];
            energy[left] = energy[right];
            energy[right] = temp;
            left++;
            right--;
        }
        int res = Integer.MIN_VALUE;
        int[] nums = new int[k];
        for (int i = 0; i+k-1 < energy.length; i+=k) {
            int max = Integer.MIN_VALUE;
            for (int j = 0; j < k; j++) {
                nums[j]+=energy[i+j];
                max = Math.max(max,nums[j]);
            }
            res = Math.max(res,max);
        }
        int n = energy.length%k;
        if(n>0){
            for (int i = 0; i < n; i++) {
                nums[i] += energy[energy.length/k*k+i];
                res = Math.max(nums[i],res);
            }
        }
        return res;
    }
}
